################## R-Script ################
################ The Disagreement We Overlook: ##################
###### Examining Core and Acquaintance Networks in a Presidential Primary #####
###### Sept 2024 ######

library(ggridges)
library(dplyr)

setwd("~/Library/CloudStorage/Dropbox")
my_data <- read.csv("")

library(ggplot2)

##### Figure 1 #####

plot_data2 <- my_data %>% filter(!is.na(totaldisc_w1))

# ALL DISCUSSANTS: Disagreement MEAN w/out title & add mean
  ggplot(filter(plot_data2, pid7_w1 <4), aes(x = disagreement_mean, y = factor(totaldisc_w1))) +
  geom_density_ridges(quantile_lines=TRUE, quantile_fun=function(disagreement_mean,...)mean(disagreement_mean)) + 
  xlab("Frequency of Disagreement") + ylab("Total Number of Discussants") +
  scale_x_continuous(limits = c(0, 6), breaks = 1:5) +
  theme_classic()

# DEM DISCUSSANTS DISAGREEMENT w/out title & add mean
  ggplot(filter(plot_data2, pid7_w1 <4), aes(x = disagreement_mean, y = factor(totaldemdisc_w1))) +
  stat_density_ridges(quantile_lines=TRUE, quantile_fun=function(disagreement_mean,...)mean(disagreement_mean)) + 
  xlab("Frequency of Disagreement") + ylab("Number of Democratic Discussants") +
  scale_x_continuous(limits = c(0, 6), breaks = 1:5) +
  theme_classic()

##### Figure 2 #####
  
# ALL DISCUSSANTS:  MEAN Similarity
    ggplot(filter(plot_data2, pid7_w1 <4), aes(x = similarity_mean, y = factor(totaldisc_w1))) +
    geom_density_ridges(quantile_lines=TRUE, quantile_fun=function(similarity_mean,...)mean(similarity_mean)) + 
    xlab("Perceived Opinion Similarity") + ylab("Total Number of Discussants") +
    scale_x_continuous(limits = c(0, 7), breaks = 1:6) +
    theme_classic()
  
# DEM DISCUSSANTS:  MEAN Similarity
    ggplot(filter(plot_data2, pid7_w1 <4), aes(x = similarity_mean, y = factor(totaldemdisc_w1))) +
    geom_density_ridges(quantile_lines=TRUE, quantile_fun=function(similarity_mean,...)mean(similarity_mean)) + 
    xlab("Perceived Opinion Similarity") + ylab("Number of Democratic Discussants") +
    scale_x_continuous(limits = c(0, 7), breaks = 1:6) +
    theme_classic()	
  
##### Figure 3 #####

plot_data1 <- my_data1 %>% filter(!is.na(big_four))
    
# Disagreement - No Title w/ Mean, 1-5 scale # 
      ggplot(filter(plot_data1, pid7_w1 <4), aes(x = disagreement_mean, y = factor(big_four))) +
        stat_density_ridges(quantile_lines=TRUE, quantile_fun=function(disagreement_mean,...)mean(disagreement_mean)) +
        xlab("Frequency of Disagreement") + ylab("Primary Candidate Supported") + 
        scale_x_continuous(limits = c(0, 6), breaks = 1:5) +
        theme_classic()
    
# Similarity - No Title w/ Mean # 
      ggplot(filter(plot_data1, pid7_w1 <4), aes(x = similarity_mean, y = factor(big_four))) +
        stat_density_ridges(quantile_lines=TRUE, quantile_fun=function(similarity_mean,...)mean(similarity_mean)) +
        xlab("Percieved Opinion Similarity") + ylab("Primary Candidate Supported") +
        scale_x_continuous(limits = c(0, 7), breaks = 1:6) +
        theme_classic()

##### Figure 5 #####
dev.off()      
  
# Load Packages  =====================
library(tidyverse)

#install.packages("ggdendro")
library(ggdendro)
      
# Create group labels ====================
      
      group_labs <- c(
        `gun` = "Gun owners",
        `trump` = "Trump voters",
        `clinton` = "Clinton voters",
        `wkgov` = "Govt. employees",
        `lgbtQ` = "LGBT",
        `police` = "Police",
        `military` = "Military",
        `runoffice` = "Ran for office",
        `africanamer` = "African Americans",
        `subabuse` = "Substance abuse",
        `attendchurch` = "Church goers",
        `muslim` = "Muslims",
        `bornoutsideus` = "Immigrants",
        `unemployed` = "Recently unemployed",
        `graddegree` = "Advanced degrees"
      )  
      
# Load data  ====================
      
# CLEANED MODULES
      #Repeat all steps for each .csv file

# Biden 
merged_df <-  read_csv("Dendrogram_Biden_W1.csv") 
      
# Bloomberg
merged_df1 <-  read_csv("Dendrogram_Bloomberg_W1.csv") 
      
# Sanders
merged_df2 <-  read_csv("Dendrogram_Sanders_W1.csv") 
      
# Warren
merged_df3 <-  read_csv("Dendrogram_Warren_W1.csv") 
      
# Create correlation matrix
cor_mat  <- cor(merged_df, method = "kendall", use = "pairwise.complete.obs")
cor_mat
      
# Print correlation matrix for appendix # 
print(cor_mat)
      
      
write.table(cor_mat, "matrix.output.txt", sep="\t")
#install.packages("officer")
library(officer)
library(dplyr)
      
# Create distance matrix
dist_mat <- dist(cor_mat)
      
hc <- hclust(dist_mat)
      
cut_df <- as.data.frame(cutree(hc, k = 5))  %>%
        set_names("cut")  %>%
        rownames_to_column("group")
      
hc_list <- dendro_data(hc, type = "rectangle")
      
hc_list$labels <- left_join(hc_list$labels, cut_df, by = c("label" = "group"))  %>%
        mutate(label = recode(label, !!!group_labs))
      
# DEDROGRAM ------------
      plot_dendro <- ggplot(hc_list$segments) +
        geom_segment(aes(x = x, y = y, xend = xend, yend = yend))+
        geom_text(data = hc_list$labels,
                  aes(x, y,  label = label, colour = factor(cut)),
                  hjust = 0, size = 3, fontface = "bold") +
        scale_colour_discrete(name = "clusters") +
        labs(x= NULL, y=NULL) +
        expand_limits(y = -.1) +
        coord_flip()   +
        theme_dendro() +
        theme(legend.position = "none") +
        scale_y_reverse(expand = c(2, -.5)) 
      # scale_color_brewer(palette = "Set1")
      
      graphics.off()
      #size_dendro <- c(3.6, 3)
      #windows(size_dendro[1], size_dendro[2])
      plot_dendro
      
      
# Save  ====================
      
# write_rds(df, \"path.rds\")

  